// pages/profile/delete-account.ts
import { MerchantAPI } from '../../utils/api'

Page({
  data: {
    statusBarHeight: 0,
    formData: {
      password: '',
      reason: ''
    },
    showPassword: false,
    hasConfirmed: false
  },

  onLoad() {
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight || 0,
      showPassword: false,
      hasConfirmed: false
    });
  },

  // 输入框变化
  onInputChange(e: any) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: value
    });
  },

  // 切换密码可见性
  togglePasswordVisibility() {
    this.setData({
      showPassword: !this.data.showPassword
    });
  },

  // 切换确认勾选
  toggleConfirm() {
    this.setData({
      hasConfirmed: !this.data.hasConfirmed
    });
  },

  // 验证表单
  validateForm(): boolean {
    const { password, reason } = this.data.formData;

    if (!password.trim()) {
      wx.showToast({
        title: '请输入登录密码',
        icon: 'none'
      });
      return false;
    }

    if (!reason.trim()) {
      wx.showToast({
        title: '请填写注销原因',
        icon: 'none'
      });
      return false;
    }

    if (reason.trim().length < 10) {
      wx.showToast({
        title: '注销原因至少10个字',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.hasConfirmed) {
      wx.showToast({
        title: '请先确认注销后果',
        icon: 'none'
      });
      return false;
    }

    return true;
  },

  // 提交注销申请
  async submitDelete() {
    if (!this.validateForm()) {
      return;
    }

    // 最终确认
    wx.showModal({
      title: '最后确认',
      content: '注销账号后将无法恢复！\n\n您确定要继续吗？',
      confirmText: '确认注销',
      confirmColor: '#ff5252',
      cancelText: '再想想',
      success: async (res) => {
        if (res.confirm) {
          await this.doDeleteAccount();
        }
      }
    });
  },

  // 执行注销操作
  async doDeleteAccount() {
    wx.showLoading({
      title: '处理中...',
      mask: true
    });

    try {
      const response = await MerchantAPI.deleteAccount(this.data.formData);
      
      if (response.code === 200) {
        wx.hideLoading();
        
        wx.showModal({
          title: '注销成功',
          content: '您的账号已成功注销，感谢您的使用。',
          showCancel: false,
          confirmText: '确定',
          success: () => {
            // 清除所有本地数据
            wx.clearStorageSync();
            
            // 跳转到登录页
            wx.reLaunch({
              url: '/pages/auth/auth'
            });
          }
        });
      } else {
        wx.hideLoading();
        wx.showToast({
          title: response.msg || '注销失败',
          icon: 'none',
          duration: 2000
        });
      }
    } catch (error: any) {
      wx.hideLoading();
      console.error('注销账号失败:', error);
      
      // 处理错误信息
      let errorMsg = '注销失败，请重试';
      if (error.msg) {
        errorMsg = error.msg;
      } else if (error.data && error.data.msg) {
        errorMsg = error.data.msg;
      }
      
      wx.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 2500
      });
    }
  },

  // 返回
  goBack() {
    wx.navigateBack({
      delta: 1
    });
  }
});








